package com.lp.sys.controller;

import com.lp.sys.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/sys")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @GetMapping("/register")
    public String registerPage() {
        return "system/index/register"; // 返回注册页面
    }

    @PostMapping("/register")
    @ResponseBody
    public Map<String, Object> registerUser(@RequestParam String loginname,
                                            @RequestParam String pwd,
                                            @RequestParam String name) {
        // 调用服务层注册方法
        boolean isRegistered = registerService.registerUser(loginname, pwd, name);
        Map<String, Object> response = new HashMap<>();
        if (isRegistered) {
            response.put("success", true);
            response.put("msg", "注册成功！");
        } else {
            response.put("success", false);
            response.put("msg", "用户名已存在！");
        }
        return response;
    }

    @GetMapping("/toLogin")
    public String toLoginPage() {
        return "system/index/login"; // 返回登录页面
    }
}
